/**
 * 获取代办列表
 */
var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.get('/', function(req, res, next) {
    let userid = _.trim(String(req.query.userid));
    let role = _.trim(String(req.query.role));
    let data = {
        isSuccess: false,
        data: {},
        msg: '待办数据获取失败！'
    };
    console.log('userid:' + userid)
    let tableName2 = "equipment_maintenance";
    let whereClause = "maninchargeid=" + userid;

    let tempSql = 'Select a.*, b.name assistantname, c.name assistant1name, d.name recorder '
    + 'From event_record a left join sys_user b on a.assistant=b.id '
    + 'left join sys_user c on a.assistant1=c.id left join sys_user d on a.recorderid=d.id';

    let sql = tempSql + " WHERE maninchargeid=" + userid + " AND isfinished=0"; 
    let sql2 = "SELECT * FROM equipment_maintenance WHERE recorderid=" + userid + " AND isfinished=0";
    switch(Number(role)) {
        case 1:
        case 2:
        case 3:
            console.log("sql:" + sql)
            console.log("sql2:" + sql2)
            db.execute(sql, function(err1, rows1) {
                if(err1) {
                    return res.json(data)
                } else {
                    data.data['hitch'] = rows1;
                    console.log(rows1)
                    if(Number(role) == 1) {
                        db.execute(sql2, function(err2, rows2) {
                            if(err2) {
                                return res.json(data);
                            }  else {
                                data.data['maintain'] = rows2; 
                                data.isSuccess = true;
                                data.msg = "待办数据获取成功！";
                                console.log(data.data);
                                return res.json(data);
                            }
                        })
                    } else {
                        data.isSuccess = true;
                        data.msg = "待办数据获取成功！";
                        console.log(data.data);
                        return res.json(data);
                    }
                }
            })
            break;
        case 4:
             let sql3 = "select t8.name recorder, t7.* from " 
             +"(select t6.name corsystem, t5.* from "
             +"(select t4.name process, t3.* from "
             +"(select t2.name type, t1.* from demand_record t1 LEFT JOIN sys_demand_type t2 ON t1.typecode = t2.id where handlerid=14)"
             +"t3 left join sys_process t4 on t4.id = t3.processcode) "
             +"t5 left join sys_software_system t6 on t6.id = t5.corsystemscode) "
             +"t7 left join sys_user t8 on t7.recorderid=t8.id where t7.isfinished=0 and t7.handlerid=" + userid ;
             console.log("sql3: " + sql3)
            db.execute(sql3, function(err, rows) {
                if(err) {
                    return res.json(data);
                } else {
                    data.data["demand"] = rows;
                    data.isSuccess = true;
                    data.msg = "待办数据获取成功！";
                    return res.json(data);
                }
            })
            break;
        default:
            break;
    }
});
module.exports = router;